// miniprogram/pages/truth-adventure/index.js
var dataModel = require('../../utils/truth-adventure-data.js');
var util = require('../../utils/utils.js');
const app = getApp()
var problems
var index=0
Page({

  /**
   * 页面的初始数据
   */
  data: {
    problemType: 0,
    titles:['真心话','大冒险'],
    problem:'',
    turthItems: [],
    turthItems_drive: [],
    turthItems_stimulate: [],
    adventureItems: [],
    adventureItems_drive: [],
    adventureItems_stimulate: [],
    flag:1
  },
  //根据id获取某个真心话或者大冒险
  getlist(id) {
    var that = this

    wx.request({
      url: app.mainurl + 'hearttacklist/getList', //仅为示例，并非真实的接口地址
      data: {
        hearttackId:id
      },
      method: 'POST',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success(res) {
        var datas = res.data.data
        console.log(res)
        var datalist=[]
        datas.forEach(val => {
          datalist.push(val.name)
        })
        problems=datalist
        problems.sort(() => Math.random() - 0.5);
        index=0
        that.nextProblem()
      }
    })
  },
  gotoShareImage: function() {
    // wx.navigateTo({
    //   url: '../share-image/index?text=' + this.data.problem + '&title=' + this.data.titles[this.data.problemType],
    // })
  },
  //设置数据
  setProblems(problemType,dataType) {

    //真心话
    if(problemType==0&&dataType==1){
      problems=this.data.turthItems
    }
    if(problemType==0&&dataType==2){
      problems=this.data.turthItems_drive
    }
    if(problemType==0&&dataType==3){
      problems=this.data.turthItems_stimulate
    }
    //大冒险
    if(problemType==1&&dataType==1){
      problems=this.data.adventureItems
    }
    if(problemType==1&&dataType==2){
      problems=this.data.adventureItems_drive
    }
    if(problemType==1&&dataType==3){
      problems=this.data.adventureItems_stimulate
    }
    problems.sort(() => Math.random() - 0.5);
    index=0
    this.nextProblem()
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var problemType = options.problemType | 0;//类型
    if(problemType==0){
      wx.setNavigationBarTitle({
        title: '真心话' 
      })
    }else{
      wx.setNavigationBarTitle({
        title: '大冒险' 
      })
    }
    this.getlist(options.id);
    // var that=this
    // wx.getStorage({
    //   key: "user",
    //     success: function(res){ 
    //       that.getlist(res.data.userId);


    //     },
    //     fail() {
        
    //     }
    //   })


    // var problemType = options.problemType | 0;//类型
    // var dataType=options.dataType;//数据类型：少年，青年，老年
    // var turthItems = dataModel.getTruthItems();
    // var turthItems_drive = dataModel.getTruthItems_drive();
    // var turthItems_stimulate = dataModel.getTruthItems_stimulate();
    // var adventureItems = dataModel.getAdventureItems();
    // var adventureItems_drive = dataModel.getAdventureItems_drive();
    // var adventureItems_stimulate = dataModel.getAdventureItems_stimulate();
    // var ee = dataModel.getlist(1);
    // console.log(ee)
    // this.setData({
    //   problemType: problemType,
    //   turthItems: turthItems,
    //   turthItems_drive: turthItems_drive,
    //   turthItems_stimulate: turthItems_stimulate,
    //   adventureItems: adventureItems,
    //   adventureItems_drive: adventureItems_drive,
    //   adventureItems_stimulate: adventureItems_stimulate
    // })
    // if(problemType==0){
    //   wx.setNavigationBarTitle({
    //     title: '真心话' 
    //   })
    // }else{
    //   wx.setNavigationBarTitle({
    //     title: '大冒险' 
    //   })
    // }
    // this.setProblems(problemType,dataType)
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  nextProblem: function () {

    //var index = util.getRandom(0, problems.length-1);
    //this.animationProblem(this, 0, index, problems);
    this.setData({
      problem: problems[index]
    })
   // console.log(problems[index])
    if(problems.length-1==index){//如果到达已有长度，重置
      problems.sort(() => Math.random() - 0.5);
      index=0;
    }else{
      index++;
    }
    
  
  },

  animationProblem: function (that,count, index, problems) {
    setTimeout(function () {
      if (count < 10) {
        that.setData({
          problem: problems[count++]
        })
        that.animationProblem(that, count, index, problems);
      } else {
        that.setData({
          problem: problems[index]
        })
      }

    }, 100);
  }
})